<?php
session_start();
include("includes/session.php");
if($do == 'list'){
	$search = '';
	if((isset($_REQUEST['s']))&&($_REQUEST['s']!='')){
		$search = $_REQUEST['s'];
		$result = mysql_query("SELECT COUNT(*) FROM `product` WHERE `cid` IN (SELECT `id` FROM `contract` WHERE `name` LIKE '%$search%' OR `tax` LIKE '%$search%' OR `address` LIKE '%$search%' OR `contract` LIKE '%$search%')");
	}
	elseif(isset($_REQUEST['search'])){
		$search = $_REQUEST['search'];
		$result = mysql_query("SELECT COUNT(*) FROM `product` WHERE `cid` IN (SELECT `id` FROM `contract` WHERE `name` LIKE '%$search%' OR `tax` LIKE '%$search%' OR `address` LIKE '%$search%' OR `contract` LIKE '%$search%')");
	}
	else{
		$result = mysql_query("SELECT COUNT(*) FROM `product`");
	}
	$page = $_REQUEST['page'];
	$total = @mysql_result($result, 0, 0);  
	$limit = mysql_result(mysql_query("SELECT `value` FROM `setting` WHERE `name` = 'cover_display'"),0,0);
	$pager = Pager::getPagerData($total, $limit, $page);
	$offset = $pager->offset;
	$limit = $pager->limit;
	$page = $pager->page;
	if($offset >= 0){
		$cover_sql = mysql_query("SELECT * FROM `product` WHERE `cid` IN (SELECT `id` FROM `contract` WHERE `name` LIKE '%$search%' OR `tax` LIKE '%$search%' OR `address` LIKE '%$search%' OR `contract` LIKE '%$search%' ORDER BY `date` DESC) ORDER BY `id` DESC LIMIT $offset, $limit");
	}
	else{
		$cover_sql = mysql_query("SELECT * FROM `product` WHERE `cid` IN (SELECT `id` FROM `contract` WHERE `name` LIKE '%$search%' OR `tax` LIKE '%$search%' OR `address` LIKE '%$search%' OR `contract` LIKE '%$search%' ORDER BY `date` DESC) ORDER BY `id` DESC");
	}
	echo mysql_error();
	$contract = '';
	while($cover_rs = mysql_fetch_object($cover_sql)){
		$company = getCompany($cover_rs->cid);
		$qty = qty_print($cover_rs->quantity_t, $cover_rs->quantity_d, $cover_rs->unit);
		$cover .= '~>'.$cover_rs->cid.'~>'.$company->name.'~>'.$company->tax.'~>'.$company->address.'~>'.$cover_rs->form.'~>'.$cover_rs->serial.'~>'.$qty->f.'~>'.$qty->t.'~>'.$cover_rs->product_name;
	}
	$contract_from = $offset + 1;
	$contract_to = $offset + $limit;
	$itt->assign("cover", arrchunk($cover, 9));
	$itt->assign("cover_total", $total);
	$itt->assign("cover_from", $contract_from);
	if($contract_to < $total){
		$itt->assign("cover_to", $contract_to);
	}
	else{
		$itt->assign("cover_to", $total);
	}
	$itt->assign("cover_page", contract_page($page, $pager->numPages, $pager->page_prev, $pager->page, $pager->page_next));
	$itt->assign("cover_display", $limit);
	$itt->assign("cover_page_end", $pager->numPages);
	$itt->assign("cover_page_curr", $pager->page);
	$itt->assign("cover_page_prev", $pager->page_prev);
	$itt->assign("cover_page_next", $pager->page_next);
	$itt->assign("search", $search);
	$itt->assign("tittle", "Danh sách in bìa");
	if($_REQUEST['get'] == 'table'){
		$itt->display('templates/cover_list_table.tpl');
	}
	else{
		$itt->display('templates/cover_list.tpl');
	}
}
else{
	$itt->display('templates/cover_history.tpl');
}
?>
